package com.lanou.moving.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.NewAlbumAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.NewAlbumRoot;
import com.lanou.moving.utils.SwipeRefreshLoadingLayout;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;


/**
 * Created by 梁树珍 on 15/10/31.
 */
public class LatestMusicFragment extends BaseFragment implements SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener, AdapterView.OnItemClickListener, View.OnClickListener {
    private GridView album_gridView;
    private VolleySingleton volleySingleton;
    private NewAlbumAdapter newAlbumAdapter;
    private List<NewAlbumRoot.DataEntity> datas;
    private ImageView album_back;
    private SwipeRefreshLoadingLayout album_refreshLoadingLayout;
    private int page = 1;
    private String url;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_latest_music, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        album_gridView = (GridView) view.findViewById(R.id.album_gridView);
        album_gridView.setOnItemClickListener(this);
        album_back = (ImageView) view.findViewById(R.id.album_back);
        album_back.setOnClickListener(this);
        album_refreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.album_refreshLoadingLayout);
        url = URL.URL_XINDIE_START + page + URL.URL_XINDIE_END;
        init();
        getAlbumDatas(url);
        return view;
    }


    public void init(){
        album_gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        album_refreshLoadingLayout.setOnRefreshListener(this);
        album_refreshLoadingLayout.setOnLoadListener(this);

        // 设置刷新效果为透明色
        album_refreshLoadingLayout.setTopColor(R.color.transparent,
                R.color.transparent, R.color.transparent, R.color.transparent);
        album_refreshLoadingLayout.setBottomColor(R.color.theme, R.color.white,
                R.color.theme, R.color.white);
    }


    public void getAlbumDatas(String url){
        StringRequestUtf requestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                NewAlbumRoot albumRoot = gson.fromJson(response, NewAlbumRoot.class);
//                NewAlbumRoot albumRoot = JSON.parseObject(response, NewAlbumRoot.class);
                datas = albumRoot.getData();
                newAlbumAdapter = new NewAlbumAdapter(mContext, datas);
                album_gridView.setAdapter(newAlbumAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        volleySingleton.addQueue(requestUtf);
    }

    @Override
    public void onRefresh() {
        StringRequestUtf requestUtf = new StringRequestUtf(URL.URL_XINDIE_START + 1 + URL.URL_XINDIE_END, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                album_refreshLoadingLayout.setRefreshing(false);
                Gson gson = new Gson();
                NewAlbumRoot root = gson.fromJson(response, NewAlbumRoot.class);
                datas = root.getData();
                newAlbumAdapter.refreshDatas(datas);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(requestUtf);
    }

    @Override
    public void onLoad() {
        page++;
        String url = URL.URL_XINDIE_START + page + URL.URL_XINDIE_END;
        StringRequestUtf requestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                album_refreshLoadingLayout.setLoading(false);
                Gson gson = new Gson();
                NewAlbumRoot root = gson.fromJson(response, NewAlbumRoot.class);
                datas = root.getData();
                newAlbumAdapter.addDatas(datas);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(requestUtf);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AlbumDetailFragment fragment = new AlbumDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("albumId", Long.valueOf(datas.get(position).getAlbumId()));
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).addToBackStack(null).commit();

    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
